package kr.lgh.sms_creditcard.DB;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import kr.lgh.sms_creditcard.MainActivity;
import kr.lgh.sms_creditcard.R;
import kr.lgh.sms_creditcard.BankType.BankInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMS_Service extends BroadcastReceiver {

	SQLiteDBHelper dbHelper;
	BankInfo bankinfo;
	String s_row, s_msg, s_date;
	private static int ABOUT_NOTIFICATION = R.layout.notification;
	private static int NOTIFICATION = 1;
	final String APP_NAME = "카드정산기";
	private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
	
	@Override
	public void onReceive(Context context, Intent intent) {
		if(intent.getAction().equals(ACTION)){
			StringBuilder sb = new StringBuilder();
			Bundle bundle = intent.getExtras();
			NotificationManager mNotifyManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
			Notification noti = new Notification(R.drawable.app_image, APP_NAME, System.currentTimeMillis());

			if(bundle != null){
				Object[] pdusObj = (Object[])bundle.get("pdus");
				SmsMessage[] messages = new SmsMessage[pdusObj.length];
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
	            
				for(int i=0; i<pdusObj.length; i++){
					messages[i] = SmsMessage.createFromPdu ((byte[])pdusObj[i]); 
	                  sb.append("Received compressed SMS\n FROM ");
	                  sb.append(messages[i].getDisplayOriginatingAddress());  
	                  sb.append("-- 전화번호 --");
	                  sb.append(messages[i].getOriginatingAddress());
	                  s_row = messages[i].getOriginatingAddress();
	                  sb.append("-- 날짜 -- ");
	                  Date date = new Date(messages[i].getTimestampMillis()); 
	                  String formattedDate = formatter.format(date);
	                  s_date = formattedDate;
	  	              sb.append(formattedDate);
	                  sb.append("--Message--- ");
	                  sb.append(messages[i].getDisplayMessageBody());
	                  s_msg = messages[i].getDisplayMessageBody();
				}
			}
			Log.i("TAG", "[SMSApp] onReceiveIntent: " + sb);
			Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
			dbHelper = new SQLiteDBHelper(context);
			ArrayList<String> arNumber = dbHelper.select_CardNumberList();
			if(arNumber.contains(s_row)){
            	
            	// 대구BC
            	if(arNumber.get(0).equals(s_row)){
            		int cardT_id = dbHelper.select_CardToID("대구BC");
            		bankinfo = new BankInfo();
            		bankinfo = (BankInfo) bankinfo.divide_DaeguBC(s_msg, s_date, "대구BC", cardT_id);
            		dbHelper.insert_MsgData(bankinfo, s_date, "대구BC");
            	}
            	// DGB
            	else if(arNumber.get(1).equals(s_row)){
            		int cardT_id = dbHelper.select_CardToID("DGB");
            		bankinfo = new BankInfo();
            		bankinfo = (BankInfo) bankinfo.divide_DGBbank(s_msg, s_date, "DGB", cardT_id);
            		dbHelper.insert_MsgData(bankinfo, s_date, "DGB");
            	}
            	// KB국민체크
            	else if(arNumber.get(2).equals(s_row)){
            		int cardT_id = dbHelper.select_CardToID("KB국민체크");
            		bankinfo = new BankInfo();
            		bankinfo = (BankInfo) bankinfo.divide_KBBank(s_msg, s_date, "KB국민체크", cardT_id);
            		dbHelper.insert_MsgData(bankinfo, s_date, "KB국민체크");
            	}
            	String msg = bankinfo.getS_card() + "- ￦" + String.valueOf(bankinfo.getS_money());
            	noti.setLatestEventInfo(context, APP_NAME, msg, contentIntent);
            	mNotifyManager.notify(SMS_Service.NOTIFICATION, noti);
            }	//	end of if
			
		}
	}
}
